package com.xuecheng.media.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.xuecheng.exception.XuechengOnlineException;
import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import com.xuecheng.model.PageParams;
import com.xuecheng.model.PageResult;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.activation.MimeType;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MediaFileServiceImpl implements MediaFileService {

  @Autowired
  MediaFilesMapper mediaFilesMapper;

  @Autowired
  MinioClient minioClient;

  //普通文件桶
  @Value("${minio.bucket.files}")
  private String bucket_files;

  @Autowired
  MediaFileService currentProxy;

 @Override
 public PageResult<MediaFiles> queryMediaFiels(Long companyId, PageParams pageParams, QueryMediaParamsDto queryMediaParamsDto) {

  //构建查询条件对象
  LambdaQueryWrapper<MediaFiles> queryWrapper = new LambdaQueryWrapper<>();

  //分页对象
  Page<MediaFiles> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
  // 查询数据内容获得结果
  Page<MediaFiles> pageResult = mediaFilesMapper.selectPage(page, queryWrapper);
  // 获取数据列表
  List<MediaFiles> list = pageResult.getRecords();
  // 获取数据总数
  long total = pageResult.getTotal();
  // 构建结果集
  PageResult<MediaFiles> mediaListResult = new PageResult<>(list, total, pageParams.getPageNo(), pageParams.getPageSize());
  return mediaListResult;

 }

 /**
  * 上传文件
  *
  * @param companyId           机构id
  * @param uploadFileParamsDto 上传文件信息
  * @param localFilePath       文件磁盘路径
  * @return
  */
 @Override
 public UploadFileResultDto uploadFile(Long companyId, UploadFileParamsDto uploadFileParamsDto, String localFilePath) {

      File file = new File(localFilePath);
      if (!file.exists()) {
       XuechengOnlineException.cast("文件不存在");
      }
      //文件名称
      String filename = uploadFileParamsDto.getFilename();
      //文件扩展名
      String extension = filename.substring(filename.lastIndexOf("."));

      //文件mimeType(媒体类型）
      String mimeType = getMimeType(extension);

      //文件的md5值
      String fileMd5 = getFileMd5(file);

      //文件的默认目录
      String defaultFolderPath = getDefaultFolderPath();

      //存储到minio中的对象名（带目录）
      String objectName = defaultFolderPath + fileMd5 + extension;

      //将文件上传到minio
      boolean b = addMediaFilesToMinio(localFilePath, mimeType, bucket_files, objectName);

      //文件大小
      uploadFileParamsDto.setFileSize(file.length());

      //将文件信息存储到数据库
      MediaFiles mediaFiles = currentProxy.addMediaFilesToDb(companyId, fileMd5, uploadFileParamsDto, bucket_files, objectName);

      //准备返回数据
      UploadFileResultDto uploadFileResultDto = new UploadFileResultDto();
      BeanUtils.copyProperties(mediaFiles,uploadFileResultDto);
      return uploadFileResultDto;


 }

 /**
  * 将文件信息添加到数据库
  * @param companyId   机构id
  * @param fileMd5   文件md5值
  * @param uploadFileParamsDto   上传文件信息
  * @param bucket_files   桶
  * @param objectName   对象名称
  */
 @Transactional   //通过代理对象调用
 public MediaFiles addMediaFilesToDb(Long companyId, String fileMd5, UploadFileParamsDto uploadFileParamsDto, String bucket_files, String objectName) {
    //从数据库查询文件
    MediaFiles mediaFiles = mediaFilesMapper.selectById(fileMd5);
    if(mediaFiles == null){
     mediaFiles = new MediaFiles();
     //拷贝基本信息
     BeanUtils.copyProperties(uploadFileParamsDto,mediaFiles);
     mediaFiles.setId(fileMd5);
     mediaFiles.setFileId(fileMd5);
     mediaFiles.setCompanyId(companyId);
     mediaFiles.setUrl("/" + bucket_files + "/" + objectName);
     mediaFiles.setBucket(bucket_files);
     mediaFiles.setFilePath(objectName);
     mediaFiles.setCreateDate(LocalDateTime.now());
     mediaFiles.setAuditStatus("002003");
     mediaFiles.setStatus("1");

     //保存文件信息到文件表
     int insert = mediaFilesMapper.insert(mediaFiles);
     if(insert < 0){
        log.error("保存文件信息到数据库失败，{}",mediaFiles);
        XuechengOnlineException.cast("保存文件信息失败");
     }
     log.debug("保存文件信息到数据库成功，{}",mediaFiles);
    }
    return mediaFiles;
 }

 /**
   * 将文件写入minio
   * @param localFilePath   文件地址
   * @param mimeType   媒体类型
   * @param bucketFiles   桶
   * @param objectName  对象名称
   */
   public boolean addMediaFilesToMinio(String localFilePath, String mimeType, String bucketFiles, String objectName) {

   try {
    UploadObjectArgs testbucket = UploadObjectArgs.builder()
            .bucket(bucketFiles)
            .object(objectName)
            .filename(localFilePath)
            .contentType(mimeType)
            .build();
    minioClient.uploadObject(testbucket);
    log.debug("上传文件到minio成功，bucket：{}，objectName：{}",bucketFiles,objectName);
    System.out.println("上传成功");
    return true;
   } catch (Exception e) {
     e.printStackTrace();
     log.error("上传文件到minio出错，bucket:{},objectName:{} 错误原因：{}",bucketFiles,objectName,e.getMessage());
     XuechengOnlineException.cast("上传文件到文件系统失败");
   }
   return false;
  }



    //文件默认存储目录路径  年/月/日
  private String getDefaultFolderPath() {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      String folder = sdf.format(new Date()).replace("-","/")+"/";
      return folder;
  }


  //获取文件md5
   private String getFileMd5(File file) {
     try (FileInputStream fileInputStream = new FileInputStream(file)) {
        String fileMd5 = DigestUtils.md5Hex(fileInputStream);
        return fileMd5;
     } catch (Exception e) {
        e.printStackTrace();
        return null;
     }
  }

  //获取媒体类型(mimeType)
   private String getMimeType(String extension) {
        if(extension == null){
         extension = "";
        }
        //根据扩展名取出mimeTyoe
        ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension);
        //通用mimeType，字节流
        String mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
        if(extensionMatch != null){
          mimeType = extensionMatch.getMimeType();
        }
        return mimeType;

   }

    /**
     * 根据媒资id查询
     *
     * @param mediaId
     * @return
     */
    @Override
    public MediaFiles getFileById(String mediaId) {
        MediaFiles mediaFiles = mediaFilesMapper.selectById(mediaId);
        return mediaFiles;
    }
 }
